<?php
/**
 * Yii view index
 *
 * @author nguyenmanhluu
 * @since Aug 14, 2013 - 3:56:48 PM
 */
$NoPhotoUrl  = Yii::app()->controller->assetDirWeb . "/img/no-image.png";
?>
<div class="box span12">
    <div class="box-header">
        <h2>
            <i class="icon-cogs"></i>
            Config <?php echo $this->optsCate ?>
        </h2>        
    </div>
    <div class="box-content">
        <?php
        $formOptions = array(
            'id'                     => 'option-form',
            'enableClientValidation' => true,
//            'enableAjaxValidation' => true,
            'htmlOptions'            => array(
                'class'   => 'form-horizontal',
                'enctype' => 'multipart/form-data',
            ),
            'clientOptions'          => array(
                'validateOnSubmit' => true,
            ),
        );

        $form = $this->beginWidget('CActiveForm', $formOptions);
        ?>
        <fieldset>
            <?php foreach ($options as $option): ?>
                <?php
                $help    = "";
                $preview = "";
                $link    = "";
                $input   = "";
                /**
                 * ID của phần Input, phục vụ cho việc áp dụng các sự kiện js cho nó
                 */
                $inputID = $option->id;
                // Label lấy từ trường description
                $label   = CHtml::label($option->description, $inputID, array(
                            'class' => 'control-label'
                ));
                $value   = $option->value;

                $htmlOptions = array(
                    'id'    => $inputID,
                    'name'  => "Options[$inputID]",
                    'class' => "span10",
                );
                /**
                 *  Nếu có truyền vào loại Input thì dùng loại đó nếu không thì 
                 *  dùng textField
                 */
                switch ($option->data_type)
                {
                    case "editor":
                        $editor_ids[] = $inputID;
                        $fieldType    = 'textArea';
                        break;

                    case "image":
                        /**
                         * Hiển thị ảnh được chọn
                         */
                        $imgSrc    = $value != '' ? Yii::app()->baseUrl . "/$value" : $NoPhotoUrl;
                        $imgOption = array(
                            'id'    => "prv_$inputID",
                            'style' => 'max-height:150px;',
                            'class' => 'tl',
                            'rel'   => 'post',
                        );
                        $preview   = CHtml::image($imgSrc, $help, $imgOption) . "<br/>";
                        $input     = "<input type='file' name='Options[$inputID]'/>";
                        break;

                    case "textArea":
                        $fieldType            = 'textArea';
                        break;
                    case "number":
                        $htmlOptions['class'] = 'number_input';
                        $fieldType            = 'textField';
                        break;
                    case "alpha":
                        $htmlOptions['class'] = 'alpha_input';
                        $fieldType            = 'textField';
                        break;
                    case "date":
                        $htmlOptions['class'] = 'datepicker';
                        $fieldType            = 'textField';
                        break;
                    case "alphanumeric":
                        $htmlOptions['class'] = 'alphanumeric_input';
                        $fieldType            = 'textField';
                        break;
                    case "tags":
                        $htmlOptions['class'] = 'tags_input';
                        $fieldType            = 'textField';
                        break;

                    default:
                        $fieldType = 'textField';
                        break;
                }

                if ($input == '')
                {
                    $input = $form->{$fieldType}($option, "value", $htmlOptions);
                }
                ?>

                <div class="control-group">
                    <?php echo $label; ?>
                    <div class ="controls">
                        <?php echo $preview; ?>
                        <?php echo $input; ?>
                        <?php echo $link; ?>
                    </div>
                </div>

            <?php endforeach; ?>    
            <hr/>
            <div class="control-group">
                <label class="control-label">Business Gallery</label>
                <div class ="controls">
                    <div class="row-fluid">
                        <?php
                        $gallery = BusinessGallery::getDefaultGallery();
                        if ($gallery):
                            ?>
                            <?php $i = 0; ?>
                            <?php foreach ($gallery as $v): ?>
                                <?php $i++; ?>
                                <div class="span2">
                                    <div class="row-fluid">
                                        <div class="span12">
                                            <div style="width: 120px;height: 100px">
                                                <img src="<?php echo $v->publicThumb ?>" alt="Merchant photo"
                                                     style="max-width: 100px;max-height: 100px" />
                                            </div>                        
                                            <input id="Pic<?php echo $v->id ?>"type="checkbox" name="deleteGallery[]" 
                                                   value="<?php echo $v->id ?>"/>
                                            <label for="Pic<?php echo $v->id ?>">Delete</label>
                                        </div>
                                        <div class="clear"></div>
                                    </div>
                                </div>
                                <?php if ($i % 6 == 0): ?>
                                </div>
                                <div class="row-fluid" style="margin-top: 10px">
                                <?php endif; ?>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                    <div class="row-fluid">
                        <h3>Upload Photo</h3>
                        <p class="muted">Recommended resolution: 240px X 240px for grid style</p>
                        <input type="file" name="galleryImage[]" multiple />
                    </div>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Content file robots.txt</label>
                <div class ="controls">
                    <?php
                    $content = '';
                    $file    = 'robots.txt';
                    if (file_exists($file))
                    {
                        $content = file_get_contents($file);
                    }
                    ?>
                    <?php echo CHtml::textArea('robots', $content, array('class' => 'input-xlarge span12', 'rows' => 12)) ?>
                </div>
            </div>
            <div class="control-group">       
                <div class ="controls">
                    <button class="btn btn-primary">Save</button>
                    <?php
//                    echo CHtml::ajaxSubmitButton("Save AJAX", '', array(
//                        'success' => "function(data) {formSuccess(data);}",
//                        ), array(
//                        'id' => 'submitItem',
//                        'name' => 'submitItem',
//                        'class' => 'btn btn-primary'
//                    ));
                    ?>                    
                </div>
            </div>
        </fieldset>
        <?php $this->endWidget(); ?>
    </div>
</div>
<script>
    function formSuccess(data)
    {
        alert('test');
    }
</script>